package cn.jcby.app.lebu.service.impl;

import cn.jcby.app.config.WxConfig;
import cn.jcby.app.excpt.enums.ExceptionMsgEnum;
import cn.jcby.app.lebu.controller.req.BasicListReq;
import cn.jcby.app.lebu.controller.req.RechargeReq;
import cn.jcby.app.lebu.dao.DictDao;
import cn.jcby.app.lebu.dao.UserChargeWithdrawalDao;
import cn.jcby.app.lebu.dao.UserDao;
import cn.jcby.app.lebu.entity.DictEntity;
import cn.jcby.app.lebu.entity.UserChargeWithdrawalEntity;
import cn.jcby.app.lebu.entity.UserEntity;
import cn.jcby.app.lebu.enums.OperationTypeEnum;
import cn.jcby.app.lebu.enums.OrderStatusEnum;
import cn.jcby.app.lebu.service.PayService;
import cn.jcby.app.lebu.service.UserChargeWithdrawalService;
import cn.jcby.app.lebu.utils.IPUtil;
import cn.jcby.app.lebu.utils.PageUtils;
import cn.jcby.app.lebu.utils.UserInfoUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

@Service("userChargeWithdrawalService")
public class UserChargeWithdrawalServiceImpl extends ServiceImpl<UserChargeWithdrawalDao, UserChargeWithdrawalEntity> implements UserChargeWithdrawalService {

    @Autowired
    private WxConfig wxConfig;
    @Autowired
    private DictDao dictDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private PayService payService ;

    @Override
    public PageUtils queryPage(BasicListReq params) {
        IPage<UserChargeWithdrawalEntity> page = this.page(
                new Page<>(params.getPage(),params.getLimit()),
                Wrappers.<UserChargeWithdrawalEntity>lambdaQuery()
                        .eq(UserChargeWithdrawalEntity::getUserId, UserInfoUtil.getUserId())
                        .in(UserChargeWithdrawalEntity::getOperationType, OperationTypeEnum.RECHARGE.getCode(),OperationTypeEnum.WITHDRAW.getCode())
                        .in(UserChargeWithdrawalEntity::getOrderStatus, OrderStatusEnum.RECHARGE_SUCCESS.getCode(),OrderStatusEnum.WITHDRAWAL_SUCCESS.getCode())
        );

        return new PageUtils(page);
    }
    @Override
    public void add(UserChargeWithdrawalEntity userChargeWithdrawalReq){
        UserChargeWithdrawalEntity userChargeWithdrawal = new UserChargeWithdrawalEntity() ;
        save(userChargeWithdrawal) ;
    }

    @Override
    public void updateBy(UserChargeWithdrawalEntity userChargeWithdrawalReq){
        UserChargeWithdrawalEntity userChargeWithdrawal = new UserChargeWithdrawalEntity() ;
        updateById(userChargeWithdrawal) ;
    }

    @Override
    public Map<String, String> recharge(RechargeReq req, HttpServletRequest request) throws Exception {

        DictEntity dict = dictDao.selectByCode(req.getCode()) ;
        Assert.isFalse(dict == null, ExceptionMsgEnum.RECORD_NOT_EXIST.getCode()+"") ;
        UserEntity user = userDao.selectById(UserInfoUtil.getUserId());
        // 1. 添加充值记录
        String orderNo = UUID.randomUUID().toString().replace("-","");
        UserChargeWithdrawalEntity userChargeWithdrawal = new UserChargeWithdrawalEntity() ;
        userChargeWithdrawal.setUserId(UserInfoUtil.getUserId());
        userChargeWithdrawal.setOrderCode(orderNo);
        userChargeWithdrawal.setOrderStatus(OrderStatusEnum.RECHARGE_ING.getCode());
        userChargeWithdrawal.setOperationType(OperationTypeEnum.RECHARGE.getCode());
        userChargeWithdrawal.setOperationPlat(req.getOperationPlat());
        userChargeWithdrawal.setAmount(BigDecimal.valueOf(Long.parseLong(dict.getVal())));
        BigDecimal energy = BigDecimal.valueOf(Long.parseLong(dict.getName())) ;
        userChargeWithdrawal.setEnergy(energy);
        userChargeWithdrawal.setCurrentEnergy(user.getEnergy().add(energy));
        userChargeWithdrawal.setCreateTime(new Date());

        return payService.createOrderApp("商品订单",
                orderNo,
                dict.getVal()+"",
                IPUtil.getRemoteAddress(request),
                "4");
    }

    @Override
    public void updateStatusByTradeNo(String outTradeNo, String transactionId, Integer code) {
        update(Wrappers.<UserChargeWithdrawalEntity>lambdaUpdate()
                .set(UserChargeWithdrawalEntity::getOrderStatus,code)
                .set(UserChargeWithdrawalEntity::getTransactionId,transactionId)
                .eq(UserChargeWithdrawalEntity::getOrderCode,outTradeNo)
        ) ;
    }


}